package com.ruoyi.web.controller.ClassesController;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.classes.domain.StudentClass;
import com.ruoyi.classes.service.StudentClassService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.tags.domain.Tags;
import com.ruoyi.tags.service.ITagsService;
import com.ruoyi.web.controller.common.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.classes.domain.Classes;
import com.ruoyi.classes.service.IClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/classes/classes")
public class ClassesController extends BaseController
{
    @Autowired
    private IClassesService classesService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITagsService iTagsService;

    @Autowired
    private StudentClassService studentClassService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes)
    {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes)
    {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        List<Classes> classes = classesService.selectClassesByTagsId(id);
        return success(classes);
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes)
    {
        return toAjax(classesService.insertClasses(classes));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes)
    {
        System.err.println(classes);
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('classes:classes:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(classesService.deleteClassesByIds(ids));
    }


    @GetMapping("/text")
    public AjaxResult selectClassesInTags(@RequestParam(value = "name",required = false,defaultValue = "") String text){
        System.out.println(text);
        List<Classes> classes = classesService.selectClassesByName(text);
        return success(classes);
    }

    @GetMapping("/RecommendList")
    public R RecommendList(){
        List<Classes> classes = classesService.RecommendList();
        return R.success(classes);
    }

    @GetMapping("/INGList")
    public  R INGList(){
        Long userId = SecurityUtils.getUserId();
        List<StudentClass> studentClasses = studentClassService.INGList(userId);
        return R.success(studentClasses);
    }

    @GetMapping("/BaoMingList")
    public R BaoMingList(){
        Long userId = SecurityUtils.getUserId();
        List<StudentClass> studentClasses = studentClassService.BaoMing(userId);
        return R.success(studentClasses);
    }

//    通过标签查询报名但是没有开始的课程
    @GetMapping("/NoOpen/{tags_id}")
    public R classesListForTagsWithNoOpen(@PathVariable("tags_id") Long tags_id){
        Long userId = SecurityUtils.getUserId();
        List<StudentClass> studentClasses = studentClassService.ListForTagsWithNoOpen(userId, tags_id);
        return R.success(studentClasses);
    }

    @GetMapping("/NoOpen/text")
    public R searchTextWithNoOpen(@RequestParam(value = "name",required = false,defaultValue = "") String text){
        Long userId = SecurityUtils.getUserId();
        List<StudentClass> studentClasses = studentClassService.searchTextWithNoOpen(text,userId);
        return R.success(studentClasses);
    }

    @GetMapping("/endList")
    public R endList(){
        Long userId=SecurityUtils.getUserId();
        System.out.println(userId);
        List<StudentClass> studentClasses = studentClassService.endList(userId);
        System.out.println(studentClasses);
        return R.success(studentClasses);
    }
}
